using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using PoliceBase;

namespace ReporterInterface
{
    public partial class MainForm : Form
    {
        private DBBasic dd = new DBBasic(Properties.Settings.Default.PoliceConnectionString);
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            cmboStreet.Items.AddRange(dd.GetStreetsNames());
            cmboSubarb.Items.AddRange(dd.GetSubarbsNames());
            cmboType.Items.AddRange(dd.GetCrimeTypes());

            cmboType.SelectedIndex = 0;
            cmboStreet.SelectedIndex = 0;
            cmboSubarb.SelectedIndex = 0;
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show(this, "Are you sure you want to exit?", "Exiting", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                e.Cancel = true;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            clearFields();
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            Address a = new Address((int)numNumber.Value, cmboStreet.SelectedItem.ToString(), cmboSubarb.SelectedItem.ToString());
            if (!dd.validateAddress(a))
            {
                MessageBox.Show(this, "Address is not valid", "Address validation", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Incident inc = new Incident(chckAmb.Checked, (int)numVictims.Value, (int)numParam1.Value, (int)numParam2.Value, chckFire.Checked, a, txtName.Text, (long)numID.Value, cmboType.SelectedItem.ToString(), dateTimePicker1.Value, DateTime.Now);
            if (chckAmb.Checked)
                Emergency.callAmbulence(a);
            if (chckFire.Checked)
                Emergency.callFire(a);
            int incID = dd.StoreIncident(inc);
            if (incID > 0)
            {
                dd.AllocateCars(incID);
                MessageBox.Show(this, "Incident reported successfully", "Incident reported", MessageBoxButtons.OK, MessageBoxIcon.Information);
                clearFields();
            }
        }

        private void buttonValidateAddress_Click(object sender, EventArgs e)
        {
            Address a = new Address((int)numNumber.Value, cmboStreet.SelectedItem.ToString(), cmboSubarb.SelectedItem.ToString());
            if (dd.validateAddress(a))
                MessageBox.Show(this, "Address is valid", "Address validation", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            else
                MessageBox.Show(this, "Address is not valid", "Address validation", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
        }

        private void cmboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] arr = dd.GetParamsNames(cmboType.SelectedItem.ToString());
            if (arr[0].Length > 0) lblParam1.Text = arr[0];
            if (arr[1].Length > 0) lblParam2.Text = arr[1];
        }

        private void clearFields()
        {
            numID.Value = numNumber.Value = numParam1.Value = numParam2.Value = numVictims.Value = 0;
            txtName.Text = "";
            chckAmb.Checked = chckFire.Checked = false;
        }
    }
}